Practical_1 Codes files

Open Vs code
open currency conversion folder in that vs code
create a file names server.js

paste code into server.js

1.server.js

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.post('/convert', (req, res) => {
    const amountInRs = req.body.amount_in_rs;
    // Perform the conversion (use a real exchange rate or a fixed rate for simplicity)
    const conversionRate = 0.0145;
    const amountInUsd = amountInRs * conversionRate;
    res.json({ amount_in_usd: amountInUsd });
});
app.listen(port, () => {
    console.log(`Currency conversion service running on http://localhost:${port}`);
});


Run this code
select nodejs for prompt
now it will run on localhost

Now open POSTMAN TOOL

CLick on +

Select Request as POST  (change it from GET)
Paste url http://localhost:3000/convert
select body then raw
then in box below paste
{
        “amount_in _rs”: 1000
}

Successfully output retrieved in Postman to test API.
Now, to call from 2 different platforms Java and .NET Programming Language.


1.calling in java

Create a new folder in prac 1 folder name it java_client
open this folder in vs code
create a file JavaClient.java
and paste this code

2.JavaClient.java

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;

public class JavaClient {
    public static void main(String[] args) {
        HttpClient client = HttpClient.newHttpClient();
        String uri = "http://localhost:3000/convert";
        String requestBody = "{ \"amount_in_rs\": 1000 }";

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(uri))
                .header("Content-Type", "application/json")
                .POST(BodyPublishers.ofString(requestBody))
                .build();

        client.sendAsync(request, BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .thenAccept(System.out::println)
                .join();
    }
}

Run without debugging select java for prompt.
Click View > Terminal:
PS C:\Users\Kishore\Documents\Cloud_Computing_Practical1\Java_Client> javac JavaClient.java
PS C:\Users\Kishore\Documents\Cloud_Computing_Practical1\Java_Client> java JavaClient      
{"amount_in_usd":14.5}
The Output is Successful that we called the server.js with making HTTP request


3. calling in .net


Create a new folder in prac 1 folder name it .net_client
open this folder in vs code
in terminal type dotnet new console
now a Program.cs file is created automatically 
paste the following code in that  and type dotnet run

Program.cs

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        try
        {
            var client = new HttpClient();
            var apiUrl = "http://localhost:3000/convert";
            
            // Create the request body with correct JSON format
            var requestBody = new
            {
                amount_in_rs = 1000
            };
            var jsonRequestBody = System.Text.Json.JsonSerializer.Serialize(requestBody);

            // Send an HTTP POST request to the API with the request body
            var response = await client.PostAsync(apiUrl, new StringContent(jsonRequestBody, Encoding.UTF8, "application/json"));

            // Check the response status code
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Response Code: " + response.StatusCode);
                Console.WriteLine("Response Body: " + await response.Content.ReadAsStringAsync());
            }
            else
            {
                Console.WriteLine("Error: " + response.StatusCode);
                Console.WriteLine("Details: " + await response.Content.ReadAsStringAsync());
            }
        }
        catch (HttpRequestException e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
        }
    }
}



